package com.work.formaldehyde.activityadapter;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.work.formaldehyde.R;
import com.work.formaldehyde.activity.CheckWuLiuActivity;
import com.work.formaldehyde.activity.KeFuActivity;
import com.work.formaldehyde.activity.ShangPingActivity;
import com.work.formaldehyde.model.AllDingDanModel;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.view.RoundImageView;

import java.util.ArrayList;

public class AllDingDanAdapter extends BaseAdapter {
    ArrayList<AllDingDanModel.data> list;
    Context context;
    private LayoutInflater mInflater;
    private DetailViewHolderListener mListener;
    Activity activity;

    public AllDingDanAdapter(ArrayList<AllDingDanModel.data> list, Context context, DetailViewHolderListener mListener, Activity activity) {
        // TODO Auto-generated constructor stub
        this.list = list;
        this.context = context;
        this.activity = activity;
        this.mInflater = LayoutInflater.from(context);
        this.mListener = mListener;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list != null ? list.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (list.get(position).getOrder_status().equals("0"))
            convertView = LayoutInflater.from(context).inflate(R.layout.dfk_list_item, null);
        else if (list.get(position).getOrder_status().equals("2"))
            convertView = LayoutInflater.from(context).inflate(R.layout.dsh_list_item, null);
        else if (list.get(position).getOrder_status().equals("1"))
            convertView = LayoutInflater.from(context).inflate(R.layout.dfh_list_item, null);
        else if (list.get(position).getOrder_status().equals("3"))
            convertView = LayoutInflater.from(context).inflate(R.layout.all_list_item, null);
        else if (list.get(position).getOrder_status().equals("4"))
            convertView = LayoutInflater.from(context).inflate(R.layout.dsh_list_item, null);
        else if (list.get(position).getOrder_status().equals("5"))
            convertView = LayoutInflater.from(context).inflate(R.layout.dsh_list_item, null);
        else if (list.get(position).getOrder_status().equals("6"))
            convertView = LayoutInflater.from(context).inflate(R.layout.dsh_list_item, null);
        init i = new init();
        if (list.get(position).getOrder_status().equals("0")) {
            i.zhuijiapinglun = convertView.findViewById(R.id.quxiaodingdan);
            i.zaiciyuyue = convertView.findViewById(R.id.quzhifu);
            i.zixunkf=convertView.findViewById(R.id.zixunkf);
            i.zixunkf.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent tokf = new Intent(context, KeFuActivity.class);
                    context.startActivity(tokf);
                }
            });
        } else if (list.get(position).getOrder_status().equals("1")) {
//            i.deldingdan = convertView.findViewById(R.id.dfh);
            i.zhuijiapinglun = convertView.findViewById(R.id.daifahup);
            i.zaiciyuyue = convertView.findViewById(R.id.querenshouhuo);
            i.zixunkf=convertView.findViewById(R.id.zixunkf);
            i.zixunkf.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent tokf = new Intent(context, KeFuActivity.class);
                    context.startActivity(tokf);
                }
            });
            if(list.get(position).getOrder_status().equals("2")){
//                i.deldingdan.setVisibility(View.VISIBLE);
                i.zhuijiapinglun.setVisibility(View.GONE);
                i.zaiciyuyue.setVisibility(View.VISIBLE);
            }else{
//                i.deldingdan.setVisibility(View.GONE);
                i.zhuijiapinglun.setVisibility(View.VISIBLE);
                i.zaiciyuyue.setVisibility(View.GONE);
            }
        } else if (list.get(position).getOrder_status().equals("2")) {
            i.zixunkf=convertView.findViewById(R.id.zixunkf);
            i.zixunkf.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent tokf = new Intent(context, KeFuActivity.class);
                    context.startActivity(tokf);
                }
            });
            i.zhuijiapinglun = convertView.findViewById(R.id.chakanwuliu);
            i.zaiciyuyue = convertView.findViewById(R.id.querenshouhuo);//yth
            i.zhuijiapinglun.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent toaddress = new Intent(context, CheckWuLiuActivity.class);
                    toaddress.putExtra("dingdanhao", list.get(position).getOrder_number());
                    toaddress.putExtra("shangpingname", list.get(position).getGoods().get(0).getGoods_name())  ;
                    toaddress.putExtra("imgurl", list.get(position).getGoods().get(0).getGoods_img());
                    context.startActivity(toaddress, ActivityOptions.makeSceneTransitionAnimation(activity).toBundle());
                }
            });
        } else if (list.get(position).getOrder_status().equals("3")) {
            i.deldingdan = convertView.findViewById(R.id.deldingdan);
            i.zhuijiapinglun = convertView.findViewById(R.id.zhuijiapinglun);
            i.zaiciyuyue = convertView.findViewById(R.id.zaiciyuyue);
        }
        else if (list.get(position).getOrder_status().equals("4")) {
            i.deldingdan = convertView.findViewById(R.id.dfh);
            i.zhuijiapinglun = convertView.findViewById(R.id.chakanwuliu);
            i.zaiciyuyue = convertView.findViewById(R.id.querenshouhuo);
            i.deldingdan.setVisibility(View.VISIBLE);
            i.zhuijiapinglun.setVisibility(View.GONE);
            i.zaiciyuyue.setVisibility(View.GONE);
            i.yth=convertView.findViewById(R.id.yth);
            i.yth.setText("已申请退款");
            i.zixunkf=convertView.findViewById(R.id.zixunkf);
            i.zixunkf.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent tokf = new Intent(context, KeFuActivity.class);
                    context.startActivity(tokf);
                }
            });
        }
        else if (list.get(position).getOrder_status().equals("5")) {
            i.deldingdan = convertView.findViewById(R.id.dfh);
            i.zhuijiapinglun = convertView.findViewById(R.id.chakanwuliu);
            i.zaiciyuyue = convertView.findViewById(R.id.querenshouhuo);
            i.deldingdan.setVisibility(View.VISIBLE);
            i.zhuijiapinglun.setVisibility(View.GONE);
            i.zaiciyuyue.setVisibility(View.GONE);
            i.yth=convertView.findViewById(R.id.yth);
            i.yth.setText("已退款");
            i.zixunkf=convertView.findViewById(R.id.zixunkf);
            i.zixunkf.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent tokf = new Intent(context, KeFuActivity.class);
                    context.startActivity(tokf);
                }
            });
        }
        else if (list.get(position).getOrder_status().equals("6")) {
            i.deldingdan = convertView.findViewById(R.id.dfh);
            i.zhuijiapinglun = convertView.findViewById(R.id.chakanwuliu);
            i.zaiciyuyue = convertView.findViewById(R.id.querenshouhuo);
            i.deldingdan.setVisibility(View.VISIBLE);
            i.zhuijiapinglun.setVisibility(View.GONE);
            i.zaiciyuyue.setVisibility(View.GONE);
            i.yth=convertView.findViewById(R.id.yth);
            i.yth.setText("商家拒绝退款");
            i.zixunkf=convertView.findViewById(R.id.zixunkf);
            i.zixunkf.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent tokf = new Intent(context, KeFuActivity.class);
                    context.startActivity(tokf);
                }
            });
        }
        i.head = convertView.findViewById(R.id.head);
        i.shangpingmingzi = convertView.findViewById(R.id.shangpingmingzi);
        i.gouwuche_manjian_text = convertView.findViewById(R.id.gouwuche_manjian_text);
        i.peisongdizhi = convertView.findViewById(R.id.peisongdizhi);
        i.yuyueshijian = convertView.findViewById(R.id.yuyueshijian);
        i.gouwuche_img = convertView.findViewById(R.id.gouwuche_img);
        i.yuyueshijian.setText(list.get(position).getStart_time() + "至" + list.get(position).getEnd_time());
        i.peisongdizhi.setText(list.get(position).getUser_address());
        ApiUtils.GET_IMAGE(context, list.get(position).getGoods().get(0).getGoods_img(), i.gouwuche_img);
        i.shangpingmingzi.setText(list.get(position).getGoods().get(0).getGoods_name());
        i.gouwuche_manjian_text.setText("¥" + list.get(position).getGoods().get(0).getGoods_discount_price());
        if (list.get(position).getGoods().size() > 1) {
            for(int creview=1;creview<list.get(position).getGoods().size();creview++) {
                LinearLayout relativeLayout = new LinearLayout(context);
                LinearLayout relativeLayouts = new LinearLayout(context);
                ImageView imageView = new ImageView(context);
                TextView textView = new TextView(context);
                TextView textViews = new TextView(context);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                LinearLayout.LayoutParams lps = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                LinearLayout.LayoutParams lpimg = new LinearLayout.LayoutParams(i.gouwuche_img.getLayoutParams().width, i.gouwuche_img.getLayoutParams().height);
                LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                LinearLayout.LayoutParams lp1s = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lps.setMargins(24, 0, 0, 0);
                lpimg.setMargins(14, 0, 0, 0);
                lp.setMargins(14, 14, 0, 0);
                lp1s.setMargins(4, 4, 4, 4);
                lp1.setMargins(0, 12, 0, 0);
                relativeLayout.setLayoutParams(lp);
                relativeLayout.setOrientation(LinearLayout.HORIZONTAL);
                relativeLayouts.setLayoutParams(lps);
                relativeLayouts.setOrientation(LinearLayout.VERTICAL);
                imageView.setLayoutParams(lpimg);
                ApiUtils.GET_IMAGE(context, list.get(position).getGoods().get(creview).getGoods_img(), imageView);
                textView.setLayoutParams(lp1);
                textView.setLines(2);
                textView.setEllipsize(TextUtils.TruncateAt.END);
                textView.setText( list.get(position).getGoods().get(creview).getGoods_name());
                textView.setTextColor(context.getResources().getColor(R.color.text_black));
                textView.setTextSize(14);
                textViews.setLayoutParams(lp1);
                textViews.setText("¥" + list.get(position).getGoods().get(creview).getGoods_discount_price());
                textViews.setTextColor(context.getResources().getColor(R.color.home_bottom_text_color_ischeck));
                textViews.setTextSize(11);
                relativeLayout.addView(imageView);
                relativeLayout.addView(relativeLayouts);
                relativeLayouts.addView(textView);
                relativeLayouts.addView(textViews);
                i.head.addView(relativeLayout);
            }
        }
        convertView.setTag(i);
        mListener.setData(i, position);
        return convertView;
    }

    public static class init {
        //首页list的子控件
        public LinearLayout head;
        public TextView shangpingmingzi, gouwuche_manjian_text, yuyueshijian, peisongdizhi,yth;
        public ImageView gouwuche_img;
        public RelativeLayout deldingdan, zhuijiapinglun, zaiciyuyue;
        public LinearLayout zixunkf;
    }

    /**
     * 展示不同数据的接口
     */
    public interface DetailViewHolderListener {
        void setData(init viewHolder, int position);
    }


}

